<?php
declare(strict_types=1);


namespace App\Http\Model\BankAcount;


use Illuminate\Support\Facades\DB;
use App\Http\Library\Excel;
use App\Http\ModelDao\BankAcount\SuperviseAccountFlowDao;

class SuperviseAccountFlowModel
{
/*数据集合*/
    private static $rows;
    /*数据长度*/
    private static $rowsCount = 0;
    /*查询字段*
    /*
     * @param array $params
     *
     * return array
     */
    public static function index($params = []):array{
            $limit =  (int)$params["pagesize"];
            $offset = ((int)$params["page"]-1)*$limit;
            $where = [];
            /*监管账号*/
            if(!isEmpty($params["reg_acc_no"])){
                $where[] = ["reg_acc_no","=",$params["reg_acc_no"]];
            }
            /*城市名称*/
            if(!isEmpty($params["city_name"])){
                $where[] = ["city_name","=",$params["city_name"]];
            }
            /*主体名称*/
            if(!isEmpty($params["ledger_subject_name"])){
                $where[] = ["ledger_subject_name","=",$params["ledger_subject_name"]];
            }
            /*清算时间-区间*/
             if(!isEmpty($params["collect_ctime"]) && !isEmpty($params["collect_etime"])){
                $where[] = ["tran_date",">=",$params["collect_ctime"]];
                $where[] = ["tran_date","<=",$params["collect_etime"]];
            }
            //DB::connection()->enableQueryLog();#开启执行日志

            //download select

            $fields = DB::raw('id,concat(tran_date," ",tran_time) tran_date,tran_serial,reg_acc_no,city_name,ledger_subject_name,case tran_dir when "C" then "贷" when "D" then "借" end tran_dir,tran_amt,tag,rcv_pay_acc,rcv_pay_name,rcv_pay_ebk,rcv_pay_ebb');

            if($params["download"] == 'All'){
                /*获取全部数据*/
                self::$rows = DB::table("pfs_query_host_detail")->wherein('pfs_dw_id',$params['pfs_dw_id'])->select($fields)->where($where)->orderBy("id","desc")->get();
                self::$rowsCount = sizeof(self::$rows);
            }else{
                self::$rows = DB::table("pfs_query_host_detail")->wherein('pfs_dw_id',$params['pfs_dw_id'])->select($fields)->where($where)->orderBy("id","desc")->offset($offset)->limit($limit)->get();
                self::$rowsCount = DB::table("pfs_query_host_detail")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->count();
            }

            //print_r(DB::getQueryLog());   //获取查询语句、参数和执行时间

        self::download($params);

        return [
            'list' => self::$rows,
            'allRows' => self::$rowsCount,
        ];
    }
    /*
     * 数据下载
    */
    private static function download($params){
        //
        if(in_array($params["download"],['All','Current'])){
            Excel::$download = true;
            Excel::$rowWidth = [
                'A:A' => 12,
                'B:B' => 16,
                'C:C' => 22,
                'D:D' => 15,
                'E:E' => 11,
                'F:F' => 14,
                'G:G' => 19,
                'H:H' => 17,
                'I:I' => 14,
                'J:J' => 14,
                'K:K' => 17,
                'L:L' => 19,
                'M:M' => 19,
                'N:N' => 12,
            ];
            $title = ['清算时间','资金流水号','监督账号','城市','主体','借/贷','交易金额','清算摘要','收付方账号','首付方名称','收付方开户行','收付方联行号'];
            //$title = ['a','b','c','d'];
            Excel::$title = $title;

           //var_dump(ClassTransactionDetailDao::numberResult(self::$rows));exit();
            Excel::send(SuperviseAccountFlowDao::numberResult(self::$rows),'监管账户流水-'.date("Ymd-His").'.xlsx');
             exit();
        }
    }
}
